class_name StackEffectComponent
extends GameplayEffectComponent

## 堆叠数
var stack_count: int = 0 :
	set(value):
		stack_count  = clamp(value, 1, component_asset.max_stack_count)


func get_component_name() -> String:
	return "StackEffectComponent"


func _on_attach(_source: Variant) -> void:
	# TODO: Stack overflow.
	var old_stack_count = stack_count
	stack_count += component_asset.stack_count_variation

	var stack_count_variation = stack_count - old_stack_count
	if stack_count_variation:
		parent.on_stack_update(stack_count_variation)


func _on_update(_delta: float) -> void:
	pass


func _on_periodic_tick() -> void:
	pass


func _on_duration_end() -> void:
	if component_asset.stack_update_policy == StackEffectComponentAsset.StackUpdatePolicy.DurationEnd:
		var old_stack_count = stack_count
		
		match component_asset.stack_update_type:
			StackEffectComponentAsset.StackUpdateType.Increase:
				stack_count += component_asset.stack_count_variation
			StackEffectComponentAsset.StackUpdateType.Decrease:
				stack_count -= component_asset.stack_count_variation
			_:
				Logger.error("Invalid stack update type.")
				return
		
		var stack_count_variation = stack_count - old_stack_count
		if stack_count_variation:
			parent.on_stack_update(stack_count_variation)


func _on_stack_update(_variation: int) -> void:
	pass


func _on_detach(_source: Variant) -> void:
	pass
